Python Starter Guide: From Novice to Coder: Python Programming for Absolute Beginners: Your Gateway to Coding Success by Svekis Sebastian & Svekis Laurence
Author:Svekis, Sebastian & Svekis, Laurence
Language: eng
Format: epub, pdf
Published: 2023-09-26T00:00:00+00:00
When you run this code, you will see the following output:
{}
{}
<class 'dict'>
<class 'dict'>
In summary, this code demonstrates how to create empty dictionaries in Python using both the dict() constructor and curly braces {}, and it also shows how to check the data type of a variable using the type() function.
You can also set values when the dictionary is being created by adding them in as name pair values and comma separating them.
dictionary1 = {"first":"Hello World","num":100,"num":200,"num":300,"boo":True}
print(dictionary1)
output:
{'first': 'Hello World', 'num': 300, 'boo': True}
To return a value from the dictionary use the dictionary variable name and within the square brackets the key name.
dictionary1 = {"first":"Hello World","num":100,"num":200,"num":300,"boo":True}
print(dictionary1['num'])
Output :
300
Using the dictionary and the key name within the square brackets new values can be assigned to the keys.
dictionary1 = {"first":"Hello World","num":100,"num":200,"num":300,"boo":True}
print(dictionary1['num'])
dictionary1['num'] = 1
print(dictionary1['num'])
dictionary1['first'] = 'Laurence'
dictionary1['last'] = 'Svekis'
print(dictionary1)
This Python code demonstrates the creation and manipulation of a dictionary. Let's break down each part of the code:
dictionary1 = {"first": "Hello World", "num": 100, "num": 200, "num": 300, "boo": True}: This line initializes a dictionary named dictionary1 with multiple key-value pairs. The keys are strings, and the values can be of different types. Note that the key "num" is repeated three times, which is not allowed in a dictionary. The later assignments to the same key will overwrite the earlier ones.
print(dictionary1['num']): This line prints the value associated with the key 'num' in the dictionary1. However, due to the repeated assignments, the value associated with 'num' is 300, so it will print 300.
dictionary1['num'] = 1: This line assigns a new value of 1 to the key 'num' in dictionary1. This overwrites the previous value of 300.
print(dictionary1['num']): This line prints the updated value associated with the key 'num', which is now 1. It will print 1.
dictionary1['first'] = 'Laurence': This line assigns a new value of 'Laurence' to the key 'first' in dictionary1. This overwrites the previous value 'Hello World'.
dictionary1['last'] = 'Svekis': This line adds a new key-value pair to dictionary1, where the key is 'last' and the value is 'Svekis'.
print(dictionary1): This line prints the entire contents of dictionary1 after the modifications. It will display the updated dictionary with the new values and key-value pair.
Download
Python Starter Guide: From Novice to Coder: Python Programming for Absolute Beginners: Your Gateway to Coding Success by Svekis Sebastian & Svekis Laurence.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(11922)
Hello! Python by Anthony Briggs(9387)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9353)
The Mikado Method by Ola Ellnestam Daniel Brolund(9321)
Dependency Injection in .NET by Mark Seemann(8866)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7860)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7350)
Grails in Action by Glen Smith Peter Ledbrook(7305)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7126)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5969)
Kotlin in Action by Dmitry Jemerov(4653)
Practical Vim (for Kathryn Amaral) by Drew Neil(3733)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3601)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3447)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3252)
Learning Java by Patrick Niemeyer & Daniel Leuck(2874)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2846)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2837)
Microservices with Go by Alexander Shuiskov(2628)